using BW.WMS.Login;
using BW.WMS.WareHouseAllot;
using BW.WMS.WareHouseCheck;
using BW.WMS.WareHouseStatistics;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.BlobStoring.Database.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace BW.WMS.EntityFrameworkCore;

[ReplaceDbContext(typeof(IIdentityDbContext))]
[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName("Default")]
public class WMSDbContext :
    AbpDbContext<WMSDbContext>,
    ITenantManagementDbContext,
    IIdentityDbContext
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */


    #region Entities from the modules

    /* Notice: We only implemented IIdentityProDbContext and ISaasDbContext
     * and replaced them for this DbContext. This allows you to perform JOIN
     * queries for the entities of these modules over the repositories easily. You
     * typically don't need that for other modules. But, if you need, you can
     * implement the DbContext interface of the needed module and use ReplaceDbContext
     * attribute just like IIdentityProDbContext and ISaasDbContext.
     *
     * More info: Replacing a DbContext of a module ensures that the related module
     * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
     */

    // Identity
    public DbSet<IdentityUser> Users { get; set; }
    public DbSet<IdentityRole> Roles { get; set; }
    public DbSet<IdentityClaimType> ClaimTypes { get; set; }
    public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
    public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
    public DbSet<IdentityLinkUser> LinkUsers { get; set; }
    public DbSet<IdentityUserDelegation> UserDelegations { get; set; }
    public DbSet<IdentitySession> Sessions { get; set; }

    // Tenant Management
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

    #endregion

    #region 仓库调拨
    public DbSet<GoodsModel> GoodsModels { get; set; }
    public DbSet<GoodsTypeModel> GoodsTypeModels { get; set; }
    public DbSet<DoccodeModel> DoccodeModels { get; set; }
    public DbSet<AllotModel> AllotModels { get; set; }
    #endregion

    #region 仓库管理
    public DbSet<WareHouseManage.Inventory> Inventory { get; set; }
    public DbSet<WareHouseManage.InventoryTransaction> InventoryTransaction { get; set; }
    public DbSet<WareHouseManage.Location> Location { get; set; }
    public DbSet<WareHouseManage.LocationItem> LocationItem { get; set; }
    public DbSet<WareHouseManage.Material> Material { get; set; }
    public DbSet<WareHouseManage.OperationLog> OperationLog { get; set; }
    public DbSet<WareHouseManage.Warehouse> Warehouse { get; set; }
    public DbSet<WareHouseManage.Zone> Zone { get; set; }
    #endregion


    #region 仓库盘点
    public DbSet<Warehouses> Warehouses { get; set; }
    public DbSet<Department> Department { get; set; }
    public DbSet<Warehousemaster> Warehousemaster { get; set; }
    public DbSet<StockTaking> StockTaking { get; set; }
    #endregion


    #region 仓库报表


    public DbSet<SalesModel> SalesModel { get; set; }

    public DbSet<StockManagementModel> StockManagementModel { get; set; }


    public DbSet<SalesReturnsModel> SalesReturnsModel { get; set; }

    #endregion

    #region 用户角色
    public DbSet<Role> Role { get; set; }
    public DbSet<User> User { get; set; }
    #endregion

    public WMSDbContext(DbContextOptions<WMSDbContext> options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        /* Include modules to your migration db context */

        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();
        builder.ConfigureFeatureManagement();
        builder.ConfigureIdentity();
        builder.ConfigureOpenIddict();
        builder.ConfigureTenantManagement();
        builder.ConfigureBlobStoring();

        /* Configure your own tables/entities inside here */

        //builder.Entity<YourEntity>(b =>
        //{
        //    b.ToTable(WMSConsts.DbTablePrefix + "YourEntities", WMSConsts.DbSchema);
        //    b.ConfigureByConvention(); //auto configure for the base class props
        //    //...
        //});

        #region  仓库管理

        builder.Entity<WareHouseManage.Inventory>(a =>
        {
            a.ToTable(WMSConsts.DbTablePrefix + "Inventory", WMSConsts.DbSchema);
            a.ConfigureByConvention(); // auto configure for the base class props
        });

        builder.Entity<WareHouseManage.InventoryTransaction>(a =>
        {
            a.ToTable(WMSConsts.DbTablePrefix + "InventoryTransaction", WMSConsts.DbSchema);
            a.ConfigureByConvention(); // auto configure for the base class props
        });

        builder.Entity<WareHouseManage.Location>(a =>
        {
            a.ToTable(WMSConsts.DbTablePrefix + "Location", WMSConsts.DbSchema);
            a.ConfigureByConvention(); // auto configure for the base class props
        });

        builder.Entity<WareHouseManage.LocationItem>(a =>
        {
            a.ToTable(WMSConsts.DbTablePrefix + "LocationItem", WMSConsts.DbSchema);
            a.ConfigureByConvention(); // auto configure for the base class props
        });

        builder.Entity<WareHouseManage.Material>(a =>
        {
            a.ToTable(WMSConsts.DbTablePrefix + "Material", WMSConsts.DbSchema);
            a.ConfigureByConvention(); // auto configure for the base class props
        });

        builder.Entity<WareHouseManage.OperationLog>(a =>
        {
            a.ToTable(WMSConsts.DbTablePrefix + "OperationLog", WMSConsts.DbSchema);
            a.ConfigureByConvention(); // auto configure for the base class props
        });

        builder.Entity<WareHouseManage.Warehouse>(a =>
        {
            a.ToTable(WMSConsts.DbTablePrefix + "Warehouse", WMSConsts.DbSchema);
            a.ConfigureByConvention(); // auto configure for the base class props
        });

        builder.Entity<WareHouseManage.Zone>(a =>
        {
            a.ToTable(WMSConsts.DbTablePrefix + "Zone", WMSConsts.DbSchema);
            a.ConfigureByConvention(); // auto configure for the base class props
        });
        #endregion

        #region 仓库盘点
        builder.Entity<Warehouses>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Warehouses", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            b.Property(x => x.Warehousename).IsRequired().HasMaxLength(128);
            //...
        });

        builder.Entity<Department>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Department", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            b.Property(x => x.Depname).IsRequired().HasMaxLength(128);
            //...
        });

        builder.Entity<Warehousemaster>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Warehousemaster", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            b.Property(x => x.MasterName).IsRequired().HasMaxLength(128);
            //...
        });

        builder.Entity<StockTaking>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "StockTaking", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
        #endregion

        #region 仓库调拨

        builder.Entity<WareHouseAllot.GoodsModel>(a =>
        {
            a.ToTable(WMSConsts.DbTablePrefix + "GoodsModel", WMSConsts.DbSchema);
            a.ConfigureByConvention(); // auto configure for the base class props
        });

        builder.Entity<WareHouseAllot.GoodsTypeModel>(a =>
        {
            a.ToTable(WMSConsts.DbTablePrefix + "GoodsTypeModel", WMSConsts.DbSchema);
            a.ConfigureByConvention(); // auto configure for the base class props
        });

        builder.Entity<WareHouseAllot.DoccodeModel>(a =>
        {
            a.ToTable(WMSConsts.DbTablePrefix + "DoccodeModel", WMSConsts.DbSchema);
            a.ConfigureByConvention(); // auto configure for the base class props
        });

        builder.Entity<WareHouseAllot.AllotModel>(a =>
        {
            a.ToTable(WMSConsts.DbTablePrefix + "AllotModel", WMSConsts.DbSchema);
            a.ConfigureByConvention(); // auto configure for the base class props
        });
        #endregion

        #region 仓库报表
        builder.Entity<SalesModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "SalesModel", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
        builder.Entity<StockManagementModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "StockManagementModel", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props

        });
        builder.Entity<SalesReturnsModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "SalesReturnsModel", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props

        });


        #endregion

        #region 用户角色
        builder.Entity<Role>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Role", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            b.Property(x => x.RoleName).IsRequired().HasMaxLength(128);
            //...
        });

        builder.Entity<User>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "User", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            b.Property(x => x.UserName).IsRequired().HasMaxLength(128);
            //...
        });
        #endregion
    }
}
